package com.jf.exam.controller.student;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jf.exam.pojo.data.StudentDO;
import com.jf.exam.pojo.vo.BeginExamVO;
import com.jf.exam.pojo.vo.ClassVO;
import com.jf.exam.pojo.vo.ExamAnswerVO;
import com.jf.exam.pojo.vo.ExamVO;
import com.jf.exam.service.ExamService;
import com.jf.exam.utils.DataUtils;
import com.jf.exam.utils.PageBean;
import com.jf.exam.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/student/exam")
public class StudentExamController {

    @Value("#{properties['student.exam.pageSize']}")
    private int pageSize;
    @Value("#{properties['student.exam.navigatePages']}")
    private int navigatePages;

    @Autowired
    ExamService examService;


    /**
     * 根据学生的班级id查询学生的试卷列表，并排除初始化状态的试卷
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpSession session, Model model) throws Exception {
        return list(1, session, model);
    }

    /**
     * 根据学生的班级id查询学生的试卷列表，并排除初始化状态的试卷
     * SELECT * FROM exam e WHERE e.fk_status != 1 AND e.id IN(SELECT ec.fk_exam FROM exam_class ec WHERE ec.fk_class = 1)
     */
    @RequestMapping("/list/{pn}")
    public String list(@PathVariable Integer pn, HttpSession session, Model model) throws Exception {
        StudentDO student = (StudentDO) session.getAttribute("student");
        ClassVO classVO = new ClassVO();
        classVO.setId(student.getFkClass());
        classVO.setPageCode(DataUtils.getPageCode(pn + ""));
        classVO.setPageSize(pageSize);
        classVO.setSize(navigatePages);
        PageBean<ExamVO> pageBean = examService.listExamByClassId(classVO);
        model.addAttribute("pageBean", pageBean);
        return "student/exam_list";
    }


    /**
     * 开始考试
     */
    @RequestMapping("/joined/{eid}")
    public String joined(@PathVariable Integer eid, HttpSession session, Model model) throws Exception {
        StudentDO student = (StudentDO) session.getAttribute("student");
        //判断是否有参加过考试
        if (examService.hasJoined(eid, student.getId())) {
            model.addAttribute("message", "你已经参加过考试了");
            return "error";
        }
        //查询试卷的数据
        BeginExamVO beginExamVO = examService.joined(eid);
        model.addAttribute("exam", beginExamVO);
        model.addAttribute("eid", eid);

        return "student/exam_take";
    }


    @RequestMapping("/submit")
    @ResponseBody
    public Result submit(String result, HttpSession session) throws Exception {
        StudentDO student = (StudentDO) session.getAttribute("student");
        System.out.println(result);
        //json ——> java 对象
        //根据json数据来生成Java类
        //{"eid":"24","questions":[{"id":"1","type":1,"answer":"0"},{"id":"32","type":2,"answer":"0,1"},{"id":"18","type":3,"answer":"0"}]}
        ObjectMapper mapper = new ObjectMapper();
        ExamAnswerVO examAnswerVO = mapper.readValue(result, ExamAnswerVO.class);
        System.out.println(examAnswerVO);

        return examService.marking(examAnswerVO, student.getId());

    }


}
